package com.yc.service.email;

import com.yc.domain.Account;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Date;

/**
 * @author zwei
 * @create 2024-08-25 14:19
 */
@Service("transfer")        // 转账邮件策略
public class TransferEmailStrategy implements EmailStrategy {
    @Autowired
    private VelocityContext context;

    @Autowired
    @Qualifier("transferTemplate")
    private Template transferTemplate;

    @Autowired
    @Qualifier("fullDf")
    private DateFormat fullDf;

    @Autowired
    @Qualifier("partDf")
    private DateFormat partDf;

    @Override
    public String generateEmailContent(Account account, double money, int toAccountId) {
        Date d = new Date();
        context.put("accountid", account.getAccountid());
        context.put("email", account.getEmail());
        context.put("subject", "转账操作通知");
        context.put("optime", fullDf.format(d));
        context.put("money", money);
        context.put("balance", account.getBalance());
        context.put("currentDate", partDf.format(d));
        try {
            StringWriter writer = new StringWriter();
            transferTemplate.merge(context, writer);
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
